home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Applications / MacGS 2.5.2ß3 / (MacGSLib.π) / macGetPictSelDLOG.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-04-17  |  9.5 KB  |  464 lines  |  [TEXT/R*ch]

  1. /* Copyright (C) 1993 Aladdin Enterprises.  All rights reserved.
  2.    Distributed by Free Software Foundation, Inc.
  3.  
  4. This file is part of Ghostscript.
  5.  
  6. Ghostscript is distributed in the hope that it will be useful, but
  7. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  8. to anyone for the consequences of using it or for whether it serves any
  9. particular purpose or works at all, unless he says so in writing.  Refer
  10. to the Ghostscript General Public License for full details.
  11.  
  12. Everyone is granted permission to copy, modify and redistribute
  13. Ghostscript, but only under the conditions described in the Ghostscript
  14. General Public License.  A copy of this license is supposed to have been
  15. given to you along with Ghostscript so you can know your rights and
  16. responsibilities.  It should be in a file named COPYING.  Among other
  17. things, the copyright notice and this notice must be preserved on all
  18. copies.  */
  19.  
  20. #include <stdio.h>
  21. #include <stddef.h>
  22. #include "gx.h"
  23. #include "gxdevice.h"
  24. #include "gp_mac.h"
  25. #include "gmacres.h"
  26. #include "gp_macui.h"
  27.  
  28.  
  29. enum
  30. {
  31.     iOKOutline = Cancel + 1,
  32.     iPicture,
  33.  
  34.     __iLastDialogItem__
  35. };
  36.  
  37.  
  38. static PicHandle    hPicture;        //    used by drawPreview
  39. static Rect            pictBounds;        //    used by MyDlgFilter
  40. static Rect            selRect;        //    used by MyDlgFilter
  41. static Boolean        fHaveSelRect;    //    used by MyDlgFilter
  42.  
  43.  
  44. extern PicHandle    macGetPicture            (gx_device *dev);
  45. Boolean                macSavePictureSelection    (gx_device *dev);
  46. void                macCopyPictureSelection (gx_device *dev);
  47.  
  48. static Boolean        DoGetPictureSelection (gx_device *dev, Rect *pRect, Boolean *pFIsAll);
  49. static PicHandle    DoMakeClippedPicture  (gx_device *dev, Rect *pRect);
  50.  
  51.  
  52.     Boolean
  53. macSavePictureSelection (gx_device *dev)
  54.  
  55. {
  56.     Rect        rect;
  57.     Boolean        fIsAll;
  58.     Boolean        retVal;
  59.  
  60.  
  61.     if (retVal = DoGetPictureSelection (dev, &rect, &fIsAll))
  62.     {
  63.         PicHandle    hPicture;
  64.  
  65.  
  66.         if (fIsAll)
  67.             hPicture = macGetPicture (dev);
  68.         else
  69.             hPicture = DoMakeClippedPicture (dev, &rect);
  70.  
  71.         retVal = (hPicture != (PicHandle) NULL) && macSavePicture (dev, hPicture);
  72.  
  73.         if (!fIsAll && hPicture /* != (PicHandle) NULL */)
  74.             KillPicture (hPicture);
  75.     }
  76.  
  77.     return retVal;
  78. }
  79.  
  80.  
  81.     void
  82. macCopyPictureSelection (gx_device *dev)
  83.  
  84. {
  85.     Rect        rect;
  86.     Boolean        fIsAll;
  87.  
  88.  
  89.     if (DoGetPictureSelection (dev, &rect, &fIsAll))
  90.     {
  91.         PicHandle    hPicture;
  92.  
  93.  
  94.         if (fIsAll)
  95.             hPicture = macGetPicture (dev);
  96.         else
  97.             hPicture = DoMakeClippedPicture (dev, &rect);
  98.  
  99.         if (hPicture /* != (PicHandle) NULL */)
  100.         {
  101.             ZeroScrap ();
  102.  
  103.             HLock ((Handle) hPicture);
  104.             PutScrap (GetHandleSize ((Handle) hPicture), (ResType) 'PICT',
  105.                       (Ptr) *hPicture);
  106.             HUnlock ((Handle) hPicture);
  107.  
  108.             if (!fIsAll)
  109.                 KillPicture (hPicture);
  110.         }
  111.     }
  112. }
  113.  
  114.  
  115.     static Boolean
  116. DoGetPictureSelection (gx_device *dev, Rect *pRect, Boolean *pFIsAll)
  117.  
  118. {
  119.     DialogPtr        dialog;
  120.     Boolean            retVal = FALSE;
  121.     pascal Boolean    MyDlgFilter (DialogPtr theDlg, EventRecord *pEvent, short *itemHit);
  122.  
  123.  
  124.     if ((hPicture = macGetPicture (dev)) == (PicHandle) NULL ||
  125.         pRect == (Rect *) NULL ||
  126.         pFIsAll == (Boolean *) NULL)
  127.     {
  128.         return retVal;
  129.     }
  130.  
  131.     dialog = GetNewDialog (MACSAVESELECTIONDLOG, (char *) NULL, (WindowPtr) -1);
  132.  
  133.     if (dialog /* != (DialogPtr) NULL */)
  134.     {
  135.         short        item;
  136.         short        iType;
  137.         Handle        hItem;
  138.         Rect        rect;
  139.         Rect        pictRect;
  140.         long        widthP;
  141.         long        heightP;
  142.  
  143.  
  144.         fHaveSelRect = FALSE;
  145.  
  146.         /*...Prep outline field...*/
  147.  
  148.         GetDItem (dialog, iOKOutline, &iType, &hItem, &rect);
  149.  
  150.         if (iType == userItem | itemDisable)
  151.             SetDItem (dialog, iOKOutline, iType, (Handle) drawOutline, &rect);
  152.  
  153.         /*...Determine the picture's geometry...*/
  154.  
  155.         rect    = (*hPicture)->picFrame;
  156.         widthP    = rect.right  - rect.left;
  157.         heightP    = rect.bottom - rect.top;
  158.  
  159.         /*...Prep the preview...*/
  160.  
  161.         GetDItem (dialog, iPicture, &iType, &hItem, &pictRect);
  162.  
  163.         if (iType == userItem | itemDisable)
  164.         {
  165.             long        widthU    = pictRect.right  - pictRect.left;
  166.             long        heightU = pictRect.bottom - pictRect.top;
  167.             long        wPhU    = widthP * heightU;
  168.             long        wUhP    = widthU * heightP;
  169.             pascal void    drawPreview (WindowPtr dialog, short item);
  170.  
  171.  
  172.             /*...Make the preview image have the same aspect ratio as the picture...*/
  173.  
  174.             if (wPhU != wUhP)
  175.             {
  176.                 long    center;
  177.                 long    half;
  178.  
  179.  
  180.                 if (wPhU > wUhP)
  181.                 {
  182.                     center = pictRect.top + (heightU >> 1);
  183.                     half   = ((heightP * widthU) / widthP) >> 1;
  184.  
  185.                     pictRect.top    = center - half;
  186.                     pictRect.bottom = center + half;
  187.                 }
  188.                 else
  189.                 {
  190.                     center = pictRect.left + (widthU >> 1);
  191.                     half   = ((heightU * widthP) / heightP) >> 1;
  192.  
  193.                     pictRect.left  = center - half;
  194.                     pictRect.right = center + half;
  195.                 }
  196.             }
  197.  
  198.             SetDItem (dialog, iPicture, iType, (Handle) drawPreview, &pictRect);
  199.         }
  200.  
  201.         /*...Accommodate the one-pixel border...*/
  202.  
  203.         pictBounds = pictRect;
  204.         InsetRect (&pictBounds, 1, 1);
  205.  
  206.         /*...Run the modal dialog...*/
  207.  
  208.         ShowWindow (dialog);
  209.  
  210.         ModalDialog ((ProcPtr) MyDlgFilter, &item);
  211.  
  212.         DisposDialog (dialog);
  213.  
  214.         /*...Calculate the new picture viewport...*/
  215.  
  216.         if (item == OK)
  217.         {
  218.             if (fHaveSelRect && !EmptyRect (&selRect))
  219.             {
  220.                 short    widthU    = pictBounds.right  - pictBounds.left;
  221.                 short    heightU = pictBounds.bottom - pictBounds.top;
  222.                 short    xOrigin    = rect.left;
  223.                 short    yOrigin = rect.top;
  224.  
  225.  
  226.                 rect.left    = xOrigin +
  227.                               (selRect.left   - pictBounds.left) * widthP  / widthU;
  228.                 rect.right    = xOrigin +
  229.                               (selRect.right  - pictBounds.left) * widthP  / widthU;
  230.                 rect.top    = yOrigin +
  231.                               (selRect.top      - pictBounds.top ) * heightP / heightU;
  232.                 rect.bottom    = yOrigin +
  233.                               (selRect.bottom - pictBounds.top ) * heightP / heightU;
  234.  
  235.                 *pFIsAll = FALSE;
  236.             }
  237.             else
  238.             {
  239.                 rect     = (*hPicture)->picFrame;
  240.                 *pFIsAll = TRUE;
  241.             }
  242.  
  243.             *pRect = rect;
  244.  
  245.             retVal = TRUE;
  246.         }
  247.     }
  248.  
  249.     return retVal;
  250. }
  251.  
  252.  
  253.     static pascal void
  254. drawPreview (WindowPtr dialog, short item)
  255.  
  256. {
  257.     short    iType;
  258.     Handle    hItem;
  259.     Rect    rect;
  260.     GrafPtr    savePort;
  261.  
  262.  
  263.     GetPort (&savePort);
  264.     SetPort ((GrafPtr) dialog);
  265.         GetDItem (dialog, item, &iType, &hItem, &rect);
  266.         PenSize (1, 1);
  267.         FrameRect (&rect);
  268.         DrawPicture (hPicture, &pictBounds);
  269.     SetPort (savePort);
  270. }
  271.  
  272.  
  273.     static pascal Boolean
  274. MyDlgFilter (DialogPtr pDialog, EventRecord *pEvent, short *itemHit)
  275.  
  276. {
  277.     Boolean    retVal;
  278.  
  279.  
  280.     if (pEvent->what == mouseDown)
  281.     {
  282.         Point    origin;
  283.         GrafPtr    savePort;
  284.  
  285.  
  286.         GetPort (&savePort);
  287.         SetPort ((GrafPtr) pDialog);
  288.  
  289.         GetMouse (&origin);
  290.  
  291.         if (PtInRect (origin, &pictBounds))
  292.         {
  293.             Point    lastPoint = origin;
  294.  
  295.  
  296.             PenSize (1, 1);
  297.             PenMode (notPatXor);
  298.             PenPat (black);
  299.  
  300.             if (fHaveSelRect)
  301.                 FrameRect (&selRect);                //    erase
  302.  
  303.             SetRect (&selRect, origin.h, origin.v, origin.h, origin.v);
  304.             FrameRect (&selRect);
  305.  
  306.             fHaveSelRect = TRUE;
  307.  
  308.             /*...Rubberband a rectangular region...*/
  309.  
  310.             while (StillDown ())
  311.             {
  312.                 Point    aPoint;
  313.                 short    top;
  314.                 short    left;
  315.                 short    bottom;
  316.                 short    right;
  317.  
  318.  
  319.                 SystemTask ();
  320.  
  321.                 GetMouse (&aPoint);
  322.  
  323.                 if        (pictBounds.left > aPoint.h)
  324.                     aPoint.h = pictBounds.left;
  325.                 else if (pictBounds.right < aPoint.h)
  326.                     aPoint.h = pictBounds.right;
  327.  
  328.                 if        (pictBounds.top > aPoint.v)
  329.                     aPoint.v = pictBounds.top;
  330.                 else if (pictBounds.bottom < aPoint.v)
  331.                     aPoint.v = pictBounds.bottom;
  332.  
  333.                 if (EqualPt (aPoint, lastPoint))
  334.                     continue;
  335.  
  336.                 lastPoint = aPoint;
  337.  
  338.                 FrameRect (&selRect);        //    erase
  339.  
  340.                 top        = (origin.v <= aPoint.v) ? origin.v : aPoint.v;
  341.                 left    = (origin.h <= aPoint.h) ? origin.h : aPoint.h;
  342.                 bottom    = (origin.v >= aPoint.v) ? origin.v : aPoint.v;
  343.                 right    = (origin.h >= aPoint.h) ? origin.h : aPoint.h;
  344.  
  345.                 SetRect (&selRect, left, top, right, bottom);
  346.  
  347.                 FrameRect (&selRect);        //    draw
  348.             }
  349.  
  350.             PenMode (patCopy);
  351.         }
  352.  
  353.         SetPort (savePort);
  354.     }
  355.  
  356.     else if (pEvent->what == keyDown)
  357.     {
  358.         *itemHit = 0;
  359.  
  360.         switch ((pEvent->message) & charCodeMask)
  361.         {
  362.             case 0x0D:    // Return pressed or ...
  363.             case 0x03:    // ... Enter pressed
  364.             {
  365.                 *itemHit = OK;
  366.                 retVal     = TRUE;
  367.                 break;
  368.             }
  369.  
  370.             case '.':
  371.             {
  372.                 short    modifiers = pEvent->modifiers & 0xFF00;
  373.  
  374.  
  375.                 if (modifiers == cmdKey)
  376.                 {
  377.                     *itemHit = Cancel;    // Command-. pressed
  378.                     retVal     = TRUE;
  379.                 }
  380.                 break;
  381.             }
  382.  
  383.             case 0x1B:
  384.             {
  385.                 *itemHit = Cancel;    // Esc pressed
  386.                 retVal     = TRUE;
  387.                 break;
  388.             }
  389.         }
  390.  
  391.         if (*itemHit /* != 0 */)
  392.         {
  393.             short        item;
  394.             short        iType;
  395.             Handle        hItem;
  396.             Rect        rect;
  397.  
  398.  
  399.             GetDItem (pDialog, *itemHit, &iType, &hItem, &rect);
  400.  
  401.             if (iType == ctrlItem | btnCtrl)
  402.             {
  403.                 long    targetTick = Ticks + 10L;
  404.  
  405.  
  406.                 HiliteControl ((ControlHandle) hItem, inButton);
  407.  
  408.                 while (Ticks <= targetTick)        //    wait for 1/6 of a second
  409.                 {}
  410.  
  411.                 HiliteControl ((ControlHandle) hItem, inButton);
  412.             }
  413.         }
  414.     }
  415.  
  416.     return retVal;
  417. }
  418.  
  419.  
  420.     static PicHandle
  421. DoMakeClippedPicture (gx_device *dev, Rect *pRect)
  422.  
  423. {
  424.     PicHandle    hPicture   = macGetPicture (dev);
  425.     Rect        windowRect;
  426.     short        pictWidth  = pRect->right  - pRect->left;
  427.     short        pictHeight = pRect->bottom - pRect->top;
  428.     CWindowPtr    pictWin;
  429.     PicHandle    hNewPict   = (PicHandle) NULL;
  430.     GrafPtr        savePort;
  431.     short        gp_VMError (char *msg, short index);
  432.  
  433.  
  434.     SetRect (&windowRect, 0, 0, pictWidth, pictHeight);
  435.  
  436.     if (gHasColorQD)
  437.         pictWin = (CWindowPtr) NewCWindow ((Ptr) NULL, &windowRect,
  438.                                            "\p",
  439.                                            FALSE, plainDBox, (WindowPtr) NULL,
  440.                                            FALSE, 0L);
  441.     else
  442.         pictWin = (CWindowPtr) NewWindow ((Ptr) NULL, &windowRect,
  443.                                            "\p",
  444.                                            FALSE, plainDBox, (WindowPtr) NULL,
  445.                                            FALSE, 0L);
  446.  
  447.     if (pictWin == (CWindowPtr) NULL)
  448.         gp_VMError ("picture window", iPictWinCreationFailed);
  449.  
  450.     GetPort (&savePort);
  451.         SetPort ((GrafPtr) pictWin);
  452.         ClipRect (pRect);
  453.         if ((hNewPict = OpenPicture (&windowRect)) /* != (PicHandle) NULL */)
  454.         {
  455.             SetOrigin (pRect->left, pRect->top);
  456.             DrawPicture (hPicture, &(*hPicture)->picFrame);
  457.             ClosePicture ();
  458.         }
  459.         DisposeWindow ((WindowPtr) pictWin);
  460.     SetPort (savePort);
  461.  
  462.     return hNewPict;
  463. }
  464.